﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using virtuaPol.Main;
using virtuaPol.Model;

namespace virtuaPol.Net
{
    /// <summary>
    /// Interaction logic for GameWnd.xaml
    /// </summary>
    public partial class GameWnd : Window
    {

        private delegate void PlayVideoDelegate(string filename, bool fullscreen);
        private delegate void ShowMessageDelegate(string message);

        private GameManager Manager { get; set; }

        public GameWnd(CWorld world)
        {
            Closed += new EventHandler(GameWnd_Closed);

            UIDispatcher dispatcher = new UIDispatcher(this.Dispatcher);
            dispatcher.ShowMessageHook += new EventHandler<ShowMessageHookArgs>(Manager_ShowMessageHook);
            dispatcher.PlayAudioHook += new EventHandler<PlayAudioHookArgs>(Manager_PlayAudioHook);
            dispatcher.PlayVideoHook += new EventHandler<PlayVideoHookArgs>(Manager_PlayVideoHook);

            Manager = new GameManager(world, dispatcher);
            Manager.EventOccurred += new EventHandler<ManagerEventOccurredArgs>(Manager_EventOccurred);
            
            InitializeComponent();

            Manager.Start();
        }

        void GameWnd_Closed(object sender, EventArgs e)
        {
            if (Manager.IsRunning) Manager.Stop();
        }

        void Manager_PlayVideoHook(object sender, PlayVideoHookArgs e)
        {
            Controls.MediaPlayer mediaPlayer = new Controls.MediaPlayer(e.File, e.Fullscreen);
            mediaPlayer.Stopped += new EventHandler(mediaPlayer_Stopped);
            MainGrid.Children.Add(mediaPlayer);
            mediaPlayer.Play();
            // Suspend worker thread. Resume it when video stopped playing
            Manager.WorkerThread.Suspend();
        }

        void mediaPlayer_Stopped(object sender, EventArgs e)
        {
            MainGrid.Children.Remove((Controls.MediaPlayer)sender);
            Manager.WorkerThread.Resume();
        }

        void Manager_PlayAudioHook(object sender, PlayAudioHookArgs e)
        {
            //throw new NotImplementedException();
        }

        void Manager_ShowMessageHook(object sender, ShowMessageHookArgs e)
        {
            MessageBox.Show(e.Message);
        }

        void Manager_EventOccurred(object sender, ManagerEventOccurredArgs e)
        {
            MainGrid.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                (ShowMessageDelegate)delegate(string name)
                {
                    //MessageBox.Show(String.Format("Event {0} occurred.", name));
                },
                e.Name);
        }

        /// <summary>
        /// Handle all key events
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            Key key = Key.None;
            if (e.Key == Key.System)
                key = e.SystemKey;
            else
                key = e.Key;
            switch (key)
            {
                default:
                    return;
                case Key.Q:
                    // If the left Alt key is pressed, quit the application
                    if (e.KeyboardDevice.IsKeyDown(Key.LeftAlt))
                        Application.Current.Shutdown();
                    break;
                case Key.F1:
                    // Open Documentation

                    break;
                case Key.Enter:
                    
                    break;
            }
            e.Handled = true;
        }

        private void MenuItem_Exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MenuItem_Save_Click(object sender, RoutedEventArgs e)
        {
            SaveGameDlg saveGameDlg = new SaveGameDlg(Manager.World);
            saveGameDlg.ShowDialog();
            if (saveGameDlg.IsCanceled)
                return;

            MessageBox.Show("The game has been saved successfully.");
        }

        private void MenuItem_New_Click(object sender, RoutedEventArgs e)
        {
            NewGameDlg newGameDlg = new NewGameDlg();
            newGameDlg.ShowDialog();
            if (newGameDlg.IsCanceled)
                return;

            GameWnd gameWnd = new GameWnd(newGameDlg.World);
            gameWnd.Show();
            gameWnd.Focus();
            this.Close();
        }

        private void MenuItem_Load_Click(object sender, RoutedEventArgs e)
        {
            LoadGameDlg loadGameDlg = new LoadGameDlg();
            loadGameDlg.ShowDialog();
            if (loadGameDlg.IsCanceled)
                return;

            GameWnd gameWnd = new GameWnd(loadGameDlg.World);
            gameWnd.Show();
            gameWnd.Focus();
            this.Close();
        }
    }
}
